package com.googlecode.jlogindexer.client;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class JLogMessageSender {
	
	
    private Destination destination;
    private JmsTemplate jmsTemplate;
    
    private static final String HASH="#";
    
    public void publishLog(String clientId, String hostname, String fileName, Object logMessage){
    	
    	System.out.println("publishing log...."+logMessage);
    	LogMessageCreator logMessageCreator=new LogMessageCreator(clientId, hostname, fileName, logMessage);
    	
    	jmsTemplate.send(destination, logMessageCreator);
    	
    }
    
    private class LogMessageCreator implements MessageCreator{
    	
    	private String clientId;
    	private String hostname; 
    	private String fileName;
    	private Object logMessage;
    	
    	
    	public LogMessageCreator(String clientId, String hostname, String fileName, Object logMessage){
    		this.clientId=clientId;
    		this.hostname=hostname;
    		this.fileName=fileName;
    		this.logMessage=logMessage;
    	}

		public Message createMessage(Session session) throws JMSException {
			
			TextMessage textMessage= session.createTextMessage();
			StringBuilder messageBuilder=new StringBuilder(clientId).append(HASH).append(hostname).append(HASH).append(fileName).append(HASH).append(logMessage);
	        textMessage.setText(messageBuilder.toString());
	        return textMessage;
			
		}
    	
    }
    
	public Destination getDestination() {
		return destination;
	}
	public void setDestination(Destination destination) {
		this.destination = destination;
	}
	public JmsTemplate getJmsTemplate() {
		return jmsTemplate;
	}
	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

}
